<?php

namespace App\Services\Admin;

use App\Models\GoodsIntegralExpire as GoodsIntegralExpireModels;
use App\Models\GoodsIntegralUsable;
use App\Models\GoodsIntegralUsableInfo;
use App\Models\SystemSet as SystemSetModels;
use App\Models\SystemImages as SystemImages;
use App\Models\SystemMessage as SystemMessageModels;
use App\Services\Api\GoodsServices;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SystemSet extends Base
{
    private static $id = '1';

    /**
     * @param string $name 查询该表字段
     */
    public static function info($name = '')
    {
        return SystemSetModels::where('id', self::$id)->value($name);
    }

    /**
     * @param string $name 查询该表字段
     */
    public static function goods_integral_expire()
    {
        return GoodsIntegralExpireModels::where('id', self::$id)->value('expire_duration');
    }

    public static function edit($request = [])
    {
        $param = $request->all();

        $image_avatar_0 = !empty($request->file('image')) ? $request->file('image') : [];

        if (!empty($image_avatar_0)) {

            $path = $image_avatar_0->store('images/'. date('Ymd'));

            $url = "/upload/".$path;

            SystemImages::where('type',1)->update(['url'=>$url]);

        }


        $image_avatar_2 = !empty($request->file('image_2')) ? $request->file('image_2') : [];

        if (!empty($image_avatar_2)) {

            $path = $image_avatar_2->store('images/'. date('Ymd'));

            $url = "/upload/".$path;

            SystemImages::where('type',2)->update(['url'=>$url]);

        }

        $image_avatar_3 = !empty($request->file('image_3')) ? $request->file('image_3') : [];

        if (!empty($image_avatar_3)) {

            $path = $image_avatar_3->store('images/'. date('Ymd'));

            $url = "/upload/".$path;

            SystemImages::where('type',3)->update(['url'=>$url]);

        }


        $image_avatar_4 = !empty($request->file('image_4')) ? $request->file('image_4') : [];

        if (!empty($image_avatar_4)) {

            $path = $image_avatar_4->store('images/'. date('Ymd'));

            $url = "/upload/".$path;

            SystemImages::where('type',4)->update(['url'=>$url]);

        }

        unset($param['image']);
        unset($param['image_2']);
        unset($param['image_3']);
        unset($param['image_4']);
        unset($param['expire_duration']);

        SystemSetModels::where('id', self::$id)->update($param);
    }

    public static function goods_integral($request = []){

        $goods_integral_exire = (new GoodsIntegralExpireModels());

        $expire_duration = $request->expire_duration??'';

        if($expire_duration != '') {
            $expire_duration = date("Y-m-d 23:59:59",strtotime("{$expire_duration}"));
            $goods_integral_exire::where('id',self::$id)->update(['expire_duration'=>$expire_duration]);
        }

    }

    /**
     * 用戶積分清零
     */
    public static function rule_pints_cleared()
    {
        DB::beginTransaction();
        $integral = (new GoodsIntegralUsable())->where('state','1')->get();

        foreach ($integral as $v){
            $value_total = (new GoodsIntegralUsableInfo())->where('g_id',$v->id)->sum('value');

            if($v->integral != $value_total){

                $value = bcsub($v->integral, $value_total);

                if($value > 0) {
                    //插入明細
                    $usable_insert = [];
                    $usable_insert['g_id'] = $v['id'];
                    $usable_insert['status'] = 2;                                   //已過期
                    $usable_insert['value'] = $value;    //已過期
                    $usable_insert['time'] = date('Y-m-d H:i:s');
                    (new GoodsIntegralUsableInfo())->insert($usable_insert);


                    GoodsServices::goods_record($value, 3, '過期作廢', $v->user_id);
                }

            }

            $v->state = 2;
            $v->save();

        }

        DB::commit();
    }

    /**
     * 查询积分方案图片
     * type 1：积分图案
     */
    public static function system_image($type = 1)
    {
        return SystemImages::where('type',$type)->value('url');
    }

    /**
     * 查询公告消息
     * @return mixed
     */
    public static function system_message()
    {
        return (new SystemMessageModels)->first();
    }

    /**
     * 公告操作
     */
    public static function system_message_do()
    {
        $input['message_title'] = \request()->message_title??'';
        $input['meesage_content'] = \request()->meesage_content??'';
        $input['is_open'] = \request()->is_open??'';

        if(!empty(\request()->id)){
            SystemMessageModels::where('id',\request()->id)->update($input);
        }else{
            (new SystemMessageModels())->insert($input);
        }
    }
}
